package com.naiterui.ehp.bs.pc.modules.sys.controller;

import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.pc.common.controller.AbstractController;
import com.naiterui.ehp.bs.pc.modules.sys.entity.Menu;
import com.naiterui.ehp.bs.pc.modules.sys.service.IMenuService;
import io.swagger.annotations.Api;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统菜单
 */
@Validated
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends AbstractController {

  @Autowired
  private IMenuService menuService;

  /**
   * 导航菜单
   *
   * @return
   */
  @SysLog("导航菜单")
  @GetMapping("/nav")
  public List<Menu> nav() {
    Long id = SecurityUtils.getCurrentUser().getId();
    return this.menuService.getUserMenuList(id);
  }

  /**
   * 所有菜单列表
   *
   * @return
   */
  @SysLog("所有菜单列表")
  @GetMapping("/list")
  @PreAuthorize("@p.check('sys:menu:list')")
  public List<Menu> list() {
    return this.menuService.queryList();
  }

  /**
   * 查询没有隐藏的菜单
   *
   * @return
   */
  @SysLog("查询没有隐藏的菜单")
  @GetMapping("/list/nothidden")
  @PreAuthorize("@p.check('sys:menu:list')")
  public List<Menu> listNotHidden() {
    return this.menuService.queryNotHiddenList();
  }

  /**
   * 选择菜单(添加、修改菜单)
   *
   * @return
   */
  @SysLog("选择菜单(添加、修改菜单)")
  @GetMapping("/select")
  public List<Menu> select() {
    // 查询列表数据
    return this.menuService.queryNotButtonList();
  }

  /**
   * 菜单信息
   *
   * @return
   */
  @SysLog("获取菜单信息")
  @GetMapping("/info/{menuId}")
  @PreAuthorize("@p.check('sys:menu:info')")
  public Menu info(@PathVariable("menuId") Long menuId) {
    return this.menuService.getById(menuId);
  }

  /**
   * 保存
   */
  @SysLog("保存菜单")
  @PostMapping("/save")
  @PreAuthorize("@p.check('sys:menu:save')")
  public void save(@RequestBody Menu menu) {
    this.menuService.save(menu);
  }

  /**
   * 修改
   */
  @SysLog("修改菜单")
  @PostMapping("/update")
  @PreAuthorize("@p.check('sys:menu:update')")
  public void update(@RequestBody Menu menu) {
    this.menuService.updateById(menu);
  }

  /**
   * 删除
   */
  @SysLog("删除菜单")
  @GetMapping("/delete/{menuId}")
  @PreAuthorize("@p.check('sys:menu:delete')")
  public void delete(@PathVariable("menuId") long menuId) throws BusinessException {
    this.menuService.delete(menuId);
  }

}
